package com.gitee.hermer.engine.jars.app.sdk.proxy;

import java.lang.reflect.Proxy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ExecuteProxyFactory<T> implements FactoryBean<T>,ApplicationContextAware {
	
private Class<T> proxyInterface;	
	
	private ApplicationContext applicationContext;
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}


	public void setProxyInterface(Class<T> proxyInterface){
		this.proxyInterface = proxyInterface;
	}


	public Object create(){
		return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{proxyInterface}, new ExecuteProxy(applicationContext));
	}

	@Override
	public T getObject() throws Exception {
		return (T) create();
	}

	@Override
	public Class<?> getObjectType() {
		return proxyInterface;
	}

	@Override
	public boolean isSingleton() {
		return true;
	}


}
